<?php
/**********************通讯类，客户端类，远程访问类功能性工具******************************/
/**
 * [sys_get_client_ip 获取客户端IP地址]
 * @Description 获取客户端IP地址
 * @Author      FengPQ
 * @DateTime    2020-05-14T17:25:41+0800
 * @param       integer                  $type [返回类型 0 返回IP地址 1 返回IPV4地址数字]
 * @param       boolean                  $adv  [是否进行高级模式获取（有可能被伪装）]
 * @return      [type]                         [description]
 */
function sys_get_client_ip($type = 0, $adv = false)
{
    $type      = $type ? 1 : 0;
    static $ip = null;
    if ($ip !== null) {
        return $ip[$type];
    }

    if ($adv) {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos = array_search('unknown', $arr);
            if (false !== $pos) {
                unset($arr[$pos]);
            }

            $ip = trim($arr[0]);
        } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
    } elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    // IP地址合法验证
    $long = sprintf("%u", ip2long($ip));
    $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
    return $ip[$type];
}

/**
 * [sys_GetCurUrl php获取当前访问的完整url地址]
 * @Description php获取当前访问的完整url地址
 * @Author      FengPQ
 * @DateTime    2020-05-14T17:26:05+0800
 * @return      [type]                   [description]
 */
function sys_GetCurUrl()
{
    $url = 'http://';
    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
        $url = 'https://';
    }
    if ($_SERVER['SERVER_PORT'] != '80') {
        $url .= $_SERVER['HTTP_HOST'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
    } else {
        $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    }
    return $url;
}

/**
 * [getHeaders 获取表头信息]
 * @Description 方法描述
 * @Author      FengPQ
 * @DateTime    2020-07-25T16:23:25+0800
 * @return      [type]                   [description]
 */
function getHeaders()
{
    $arr = [];

    $systemParam = apache_request_headers();
    //读取配置文件中的私钥信息
    $arr['uuid']      = isset($systemParam['Tp-Uuid']) ? $systemParam['Tp-Uuid'] : ''; //用户uuid
    $arr['token']     = isset($systemParam['Tp-Token']) ? $systemParam['Tp-Token'] : ''; //最终access token结果
    $arr['timestamp'] = isset($systemParam['Tp-Timestamp']) ? $systemParam['Tp-Timestamp'] : ''; //时间戳，UTC时间，以北京时间东八区（+8）为准
    $arr['source']    = isset($systemParam['Tp-Source']) ? $systemParam['Tp-Source'] : ''; //来源 DeviceType枚举 // $this->arr['clien_ip']  = sys_get_client_ip();//留着，暂时不

    $_SESSION[SESS_HEADER] = $arr;
    log_info("headers:" . json_encode($_SESSION[SESS_HEADER]));
    return $arr;
}

/**
 * [setHeader 处理跨域问题]
 * @Description 方法描述
 * @Author      FengPQ
 * @DateTime    2020-07-25T16:27:57+0800
 */
function setHeader()
{
    header("Access-Control-Allow-Origin:*");
    header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
    header('Access-Control-Allow-Headers: *');
    header('Content-type:application/json; charset=utf-8');
}
